package com.service.tools;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.service.bean.alarm.AlarmInfo;
import com.service.bean.alarm.AlarmRule;
import com.service.bean.device.Vehicle;
import com.service.bean.signal.GpsSignal;
import com.service.calculate.vehicle.SignalCreator;
import com.service.socket.protocol.Conversion;

public class AlarmTool {

	public static AlarmInfo createAlarmInfo(Vehicle vehicle, AlarmRule rule) {
		AlarmInfo ai = new AlarmInfo();
		ai.gpsNo = vehicle.device.deviceNo;
		ai.equipmentId = vehicle.equipmentId;
		ai.alarmTime = new Date();
		ai.alarmType = rule.alarmType;
		ai.alarmSubType = rule.alarmCategory;
		ai.companyCode = vehicle.company.code;
		ai.addTime = new Date();
		return ai;
	}
	
	public static AlarmInfo createAlarmInfo(Vehicle vehicle, GpsSignal signal, AlarmRule rule) {
		AlarmInfo ai = new AlarmInfo();
		ai.signalId = signal.signalId;
		ai.gpsNo = signal.gpsNo;
		ai.equipmentId = vehicle.equipmentId;
		ai.alarmTime = signal.gpsTime;
		ai.alarmLocation = signal.baiduLocation;
		ai.areaId = signal.areaId;
		ai.alarmType = rule.alarmType;
		ai.alarmSubType = rule.alarmCategory;
		ai.companyCode = vehicle.company.code;
		ai.addTime = new Date();
		return ai;
	}

	public static String getAlarmLevel(long keepTime, AlarmRule rule) {
		String level = null;
		if (rule.alarmLevel == null || "".equals(rule.alarmLevel.trim())) {
			level = "2";
			return level;
		}
		String[] timeZones = rule.alarmLevel.split(";");
		List<String> list = new ArrayList<String>();
		String[] arrayOfString1;
		int j = (arrayOfString1 = timeZones).length;
		for (int i = 0; i < j; i++) {
			String zone = arrayOfString1[i];
			list.add(zone);
		}
		Collections.sort(list, new Comparator<String>() {
			@Override
			public int compare(String s1, String s2) {
				String[] ss1 = s1.split(",");
				String[] ss2 = s2.split(",");
				String end1 = ss1[0].split("~")[1];
				String end2 = ss2[0].split("~")[1];
				return end1.compareTo(end2);
			}
		});
		if (0L == keepTime) {
			level = ((String) list.get(0)).split(",")[1];
		} else {
			String zone = null;
			int start = 0;
			int end = 0;
			for (int i = 0; i < list.size(); i++) {
				zone = ((String) list.get(i)).split(",")[0];
				start = Integer.valueOf(zone.split("~")[0]).intValue() * 60;
				end = Integer.valueOf(zone.split("~")[1]).intValue() * 60;
				if ((keepTime >= start) && (keepTime <= end)) {
					level = ((String) list.get(i)).split(",")[1];
					break;
				}
				if (i == list.size() - 1) {
					level = ((String) list.get(i)).split(",")[1];
					break;
				}
			}
		}
		return level;
	}

	public static String getVoiceRemindingCommand(int kind) {
		StringBuffer command = new StringBuffer();
		command.append("551000D0");
		if (1 == kind) {
			command.append("01");
		} else if (2 == kind) {
			command.append("02");
		}
		command.append("00000");
		String crc = Conversion.getCrc(command.toString());
		command.append(crc + "55");
		return command.toString();
	}

	/**
	 * 1.无排班：无绑定区域不报警
	 * 2.有排班：无排班区域无绑定区域、不在排班时间内不报警
	 * @param vehicle
	 * @param signal
	 * @return false
	 */
	public static boolean calDutyAndAreaAlarm(Vehicle vehicle, GpsSignal signal) {
		if (null == vehicle || (null == vehicle.duty && null == vehicle.areaMap)) {
			return false;
		}
		if (null != vehicle.duty) {
			if ((null == vehicle.duty.areas && null == vehicle.areaMap)
					|| !SignalCreator.calIsInWorkTime(false, vehicle, signal)) {
				return false;
			}
		}
		return true;
	}
}
